/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.ui.controls.text.styled;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;

import com.xored.glance.ui.sources.ColorManager;
import com.xored.glance.ui.sources.Match;

/**
 * @author Yuri Strot
 * 
 */
public class StyledTextSource extends AbstractStyledTextSource {

	public StyledTextSource(StyledText text) {
		super(text);
	}

	@Override
	protected void doDispose() {
		super.doDispose();
		clearHighlight();
	}

	protected void clearHighlight() {
		if (previous != null && previous.length > 0) {
			for (int i = 0; i < previous.length; i++) {
				RangeGroup group = previous[i];
				getText().replaceStyleRanges(group.getStart(),
						group.getEnd() - group.getStart(), new StyleRange[0]);
				StyleRange[] ranges = group.getRanges();
				for (StyleRange range : ranges) {
					getText().replaceStyleRanges(range.start, range.length,
							new StyleRange[] { range });
				}
			}
		}
		previous = null;
	}

	public void show(Match[] matches) {
		clearHighlight();
		previous = new RangeGroup[matches.length];
		for (int i = 0; i < matches.length; i++) {
			Match match = matches[i];
			int index = match.getOffset();
			int latIndex = index + match.getLength();
			StyleRange[] matchRanges = getText().getStyleRanges(index,
					match.getLength());
			List<StyleRange> ranges = new ArrayList<StyleRange>();
			Color bg = ColorManager.getInstance().getBackgroundColor();
			for (StyleRange styleRange : matchRanges) {
				ranges.add(styleRange);
				if (styleRange.length < 0) {
					continue;
				}
				StyleRange newStyleRangerange = new StyleRange(
						styleRange.start, styleRange.length, null, bg,
						styleRange.fontStyle);
				if (styleRange.start - index > 0)
					getText().replaceStyleRanges(index,
							styleRange.start - index,
							new StyleRange[] { newStyleRangerange });
				getText().replaceStyleRanges(styleRange.start,
						styleRange.length,
						new StyleRange[] { newStyleRangerange });
				index = styleRange.start + styleRange.length;
			}
			if (latIndex - index > 0) {
				StyleRange newStyleRangerange = new StyleRange(index, latIndex
						- index, null, bg);
				getText().replaceStyleRanges(index, latIndex - index,
						new StyleRange[] { newStyleRangerange });
			}
			previous[i] = new RangeGroup(match.getOffset(), latIndex, ranges
					.toArray(new StyleRange[ranges.size()]));
		}
	}

	private RangeGroup[] previous;

}
